package com.buka.role.dao;

import com.buka.role.entity.Role;
import com.buka.role.entity.UserRole;
import com.buka.utils.JDBCUtil;

import java.util.List;

public class RoleDao {

    //新建角色
    public int add(Role role){
        String sql = "insert into role(name,code) values(?,?)";
        return JDBCUtil.executeUpdateByArr(sql,role.getName(),role.getCode());
    }

    public int delete(Long id){
        String sql = "delete from role where id = ?";
        return JDBCUtil.executeUpdateByArr(sql,id);
    }

    public int update(Role role){
        String sql = "update role set `name`=?,code=? where id = ? ";
        return JDBCUtil.executeUpdateByArr(sql,role.getName(),role.getCode(),role.getId());
    }

    //查询全部角色
    public List<Role> list(){
        String sql = "select * from role";
        return JDBCUtil.executeQuery(sql,Role.class);
    }

    public Role selectById(Long id){
        String sql = "select * from role where id = ?";
        List<Role> ts = JDBCUtil.executeQuery(sql,Role.class, id);
        //集合有可能为空
        if(null == ts || ts.isEmpty()){
            return null;
        }
        return ts.get(0);
    }


    public List<Role> selectByUserId(Long userId) {
        return null;



    }

    public List<UserRole> selectUserRoleByUid(Long userId) {
        String sql = "select * from t_user_role where uid = ?";
        return JDBCUtil.executeQuery(sql,UserRole.class, userId);
    }

    public List<Role> selectRoleByRoleIds(String ridStr) {
        //把数组或者集合转为字符串，
        String sql = "select * from role where id in (" + ridStr + ")";
        return JDBCUtil.executeQuery(sql,Role.class);
    }

    public boolean deleteByUserId(String userid) {

        String sql = "delete from t_user_role where uid = ?";
        return JDBCUtil.executeUpdateByArr(sql,userid) > 0;

    }

    public boolean insertUserRole(String userid, String roleId) {
        String sql = "insert into t_user_role(uid,rid) values(?,?)";
        return JDBCUtil.executeUpdateByArr(sql,userid,roleId) > 0;
    }
}
